﻿using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace WikiUploader.Controls
{
   /// <summary>
   /// Interaction logic for ExcerptUploaderView.xaml
   /// </summary>
   public partial class ExcerptUploaderView
   {
      private TextBox m_TxtFocused;

      public ExcerptUploaderView ()
      {
         InitializeComponent ();
         this.LoadOtherLinks (0);
      }

      private void LoadOtherLinks (int index)
      {
         this._cmbOtherLink.Items.Clear ();
         this._cmbOtherLink.Items.Add ("wikipedia");
         this._cmbOtherLink.Items.Add ("Definition");
         var otherLinksFile = new FileInfo ("OtherLinks.txt");
         if (otherLinksFile.Exists)
         {
            using (var reader = new StreamReader (otherLinksFile.FullName))
            {
               while (reader.Peek () != -1)
               {
                  this._cmbOtherLink.Items.Add (reader.ReadLine ());
               }
            }
         }
         if (index == -1)
            this._cmbOtherLink.SelectedIndex = this._cmbOtherLink.Items.Count - 1;
         else
            this._cmbOtherLink.SelectedIndex = 0;
      }

      private void CenterClick (object sender, RoutedEventArgs e)
      {
         this.AddTag ("<center>", "</center>");
      }

      private void SmallClick (object sender, RoutedEventArgs e)
      {
         this.AddTag ("<small>", "</small>");
      }

      private void BoldClick (object sender, RoutedEventArgs e)
      {
         this.AddTag ("'''", "'''");
      }

      private void ItalicClick (object sender, RoutedEventArgs e)
      {
         this.AddTag ("''", "''");
      }

      private void LinkClick (object sender, RoutedEventArgs e)
      {
         this.AddTag ("[[", "]]");
      }

      private void OtherLinkClick (object sender, RoutedEventArgs e)
      {
         if (this._cmbOtherLink.SelectedValue.ToString () == "wikipedia")
            this.AddTag ("[[wikipedia:" + this._txtWikipedia.Text + "|", "]]");
         else if (this._cmbOtherLink.SelectedValue.ToString () == "Definition")
            this.AddTag ("{{Definition|", "|" + this._txtWikipedia.Text + "}}");
         else
            this.AddTag ("[[" + this._cmbOtherLink.SelectedValue + "|", "]]");
      }

      private void BreakClick (object sender, RoutedEventArgs e)
      {
         this.AddTag ("<br />");
      }

      private void AddTag (string start, string end = "")
      {
         if (this.m_TxtFocused != null)
         {
            if (this.m_TxtFocused.SelectionLength > 0)
            {
               var lastChar = this.m_TxtFocused.Text [this.m_TxtFocused.SelectionStart + this.m_TxtFocused.SelectionLength - 1];
               if (lastChar == ' ')
                  this.m_TxtFocused.SelectionLength = this.m_TxtFocused.SelectionLength - 1;
               if (this.m_TxtFocused.Text [this.m_TxtFocused.SelectionStart] == ' ')
               {
                  this.m_TxtFocused.SelectionStart = this.m_TxtFocused.SelectionStart + 1;
                  this.m_TxtFocused.SelectionLength = this.m_TxtFocused.SelectionLength - 1;
               }
            }

            var index = this.m_TxtFocused.SelectionStart + this.m_TxtFocused.SelectionLength + start.Length + end.Length;
            var text = this.m_TxtFocused.Text.Insert (this.m_TxtFocused.SelectionStart + this.m_TxtFocused.SelectionLength, end);
            text = text.Insert (this.m_TxtFocused.SelectionStart, start);
            this.m_TxtFocused.Text = text;
            this.m_TxtFocused.SelectionStart = index;
            this.m_TxtFocused.SelectionLength = 0;
            this.m_TxtFocused.Focus ();
         }
      }

      private void TxtFocused (object sender, RoutedEventArgs e)
      {
         this.m_TxtFocused = (TextBox) sender;
      }

      private void RefreshOtherLinks (object sender, RoutedEventArgs e)
      {
         this.LoadOtherLinks (-1);
      }

      private void AddOtherLinks (object sender, RoutedEventArgs e)
      {
         var otherLinksFile = new FileInfo ("OtherLinks.txt");
         using (var writer = new StreamWriter (otherLinksFile.FullName, true))
         {
            writer.WriteLine (this._txtWikipedia.Text);
            writer.Flush ();
            writer.Close ();
         }
         this.LoadOtherLinks (-1);
      }
   }
}
